package com.example.usermanagement.liuka.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.usermanagement.exception.ObjectNotException;
import com.example.usermanagement.liuka.model.FlowPackage;
import com.example.usermanagement.liuka.service.FlowPackageService;
import com.example.usermanagement.liuka.service.FlowUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;


/**
 * 套餐包管理
 */
@RestController
@RequestMapping("/flow-package")
@Validated
public class FlowPackageController {

    private  FlowPackageService flowPackageService;

    @Autowired
    public FlowPackageController(FlowPackageService flowPackageService){
        this.flowPackageService = flowPackageService;
    }



    /**
     *  创建套餐
     * @param packageName 套餐名称
     * @param startTime 套餐活动开始日期
     * @param endTime 套餐活动结束日期
     * @param time  套餐有效期（天）
     * @param type 生效方式 1 立即生效 2 指定日期生效
     * @param carTime  指定日期
     * @param readyMode   1购买先用 2先到期先用 3 套餐总流量小先用 4 先订购先用
     * @param sort 优先级 升序排列
     * @param flowSize  套餐流量 单位（B）
    **/
    @PostMapping()
    public void addPackage(@NotBlank(message = "套餐包名称不能为空")String packageName,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime startTime,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime,
                           @NotNull(message = "有效期不能为空") Long time,
                           @RequestParam(defaultValue = "1")Integer type,
                           LocalDateTime carTime, Integer readyMode,
                           @RequestParam(defaultValue = "1")Integer sort,
                           @NotNull(message = "套餐流量不能为空")BigDecimal flowSize) throws ObjectNotException {
        FlowPackage flowPackage = flowPackageService.getOne(Wrappers.lambdaQuery(new FlowPackage())
                .eq(FlowPackage::getPackageName,packageName.trim()));
        if(Objects.nonNull(flowPackage)){
            throw new ObjectNotException("套餐包名称已存在");
        }

        flowPackage = FlowPackage.builder().packageName(packageName)
                .startTime(startTime)
                .endTime(endTime)
                .time(time)
                .type(type)
                .carTime(carTime)
                .readyMode(readyMode)
                .sort(sort)
                .flowSize(flowSize).build();
        flowPackageService.save(flowPackage);

    }


    /**
     *套餐包分页列表
     * @param page 第几页 默认1
     * @param pageSize 每页条数  默认20
     * @param packageName  套餐包名
     * @return
    **/
    @GetMapping("/page")
    public IPage<FlowPackage> getPage(@RequestParam(defaultValue = "1")Long page,
                                      @RequestParam(defaultValue = "20")Long pageSize,
                                      String packageName){
        LambdaQueryWrapper<FlowPackage> wrapper = Wrappers.lambdaQuery(new FlowPackage());
        wrapper.select(FlowPackage::getId,FlowPackage::getPackageName,FlowPackage::getCarTime);
        if(StrUtil.isNotBlank(packageName)){
            wrapper.like(FlowPackage::getPackageName,packageName);
        }
        wrapper.orderByDesc(FlowPackage::getId);
        return flowPackageService.page(new Page<>(page,pageSize),wrapper);
    }


    /**
     *  详情
     * @param id
     * @return
    **/
    @GetMapping("/{id}")
    public FlowPackage getDetail(@PathVariable Long id){
        return flowPackageService.getById(id);
    }

    /**
     * 修改套餐包
     * @param id  id
     * @param packageName 套餐名称
     * @param startTime 套餐活动开始日期
     * @param endTime 套餐活动结束日期
     * @param time  套餐有效期（天）
     * @param type 生效方式 1 立即生效 2 指定日期生效
     * @param carTime  指定日期
     * @param readyMode   1购买先用 2先到期先用 3 套餐总流量小先用 4 先订购先用
     * @param sort 优先级 升序排列
     * @param flowSize  套餐流量 单位（B）
    **/
    @PutMapping("")
    public void updateDetail(@NotNull(message = "id不能为空")Long id,
                             String packageName,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime startTime,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime endTime,
                             Long time,
                             @RequestParam(defaultValue = "1")Integer type,
                             LocalDateTime carTime, Integer readyMode,
                             @RequestParam(defaultValue = "1")Integer sort,
                            BigDecimal flowSize) throws ObjectNotException {
        FlowPackage flowPackage = flowPackageService.getById(id);
        if(Objects.isNull(flowPackage)){
            throw new ObjectNotException("套餐包不存在");
        }
        flowPackage.setPackageName(packageName);
        flowPackage.setStartTime(startTime);
        flowPackage.setEndTime(endTime);
        flowPackage.setTime(time);
        flowPackage.setType(type);
        flowPackage.setCarTime(carTime);
        flowPackage.setReadyMode(readyMode);
        flowPackage.setSort(sort);
        flowPackage.setFlowSize(flowSize);
        flowPackageService.updateById(flowPackage);

    }


    /**
     *  批量删除套餐包
     * @param ids  套餐包id
    **/
    @DeleteMapping()
    public void delByBatch(@NotNull(message = "ids不能为空") Long... ids){
        flowPackageService.removeBatchByIds(Arrays.asList(ids));
    }



}
